package com.kylin.common.weixin.pay;

import com.alibaba.fastjson2.JSONObject;
import com.kylin.common.weixin.util.WxRequest;

/**
 * Native支付
 * @author wuhao
 * @version 1.0 - 2023/5/26
 */
public class NativePayService {

    /** 下单接口 */
    private static final String NATIVE_ORDER_URL = "https://api.mch.weixin.qq.com/v3/pay/transactions/native";
    /** 订单查询 */
    private static final String NATIVE_ORDER_SEARCH_URL = "https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/";

    /**
     * 下单接口
     * @param appid 应用ID
     * @param mchid 商户ID
     * @param description 商品描述
     * @param out_trade_no 订单号
     * @param notify_url  回调地址
     * @param price 支付金额
     * @param userIp 用户终端IP
     *
     * @return
     * @author wuhao
     * @version 1.0.0 - 2023/5/26
     */
    public static JSONObject order(String appid, String mchid, String description, String out_trade_no, String notify_url, Long price, String userIp){
        JSONObject params = new JSONObject();
        params.put("appid",appid);
        params.put("mchid",mchid);
        params.put("description",description);
        params.put("out_trade_no",out_trade_no);
        params.put("notify_url",notify_url);
        JSONObject amount = new JSONObject();
        amount.put("total", price);
        params.put("amount",amount);
        JSONObject scene_info = new JSONObject();
        scene_info.put("payer_client_ip", userIp);
        params.put("scene_info", scene_info);
        return WxRequest.post(NATIVE_ORDER_URL, params.toJSONString());
    }

    /**
     * 订单查询
     * @param outTradeNo 商户订单号
     * @param mchid  商户ID
     * @return
     * @author wuhao
     * @version 1.0.0 - 2023/5/26
     */
    public static  JSONObject orderSearch(String outTradeNo, String mchid) {
        String url = NATIVE_ORDER_SEARCH_URL + outTradeNo + "?mchid=" + mchid;
        return WxRequest.get(url);
    }
}
